using System.Collections.Generic;
using MultiplayerServerDiscoveryHandlers;
using UnityEngine;

public class ServerDetectionLayer : MonoBehaviour
{
	public UIScrollView scrollView;

	public GameObject serverButtonPrefab;

	public UITable table;

	private FindServersHandler findServersHandler;

	private List<ServerDisplayButton> buttons = new List<ServerDisplayButton>();

	private void OnEnable()
	{
		BehaviourSingleton<GGUDPNetworkServer>.instance.EndServer();
		BehaviourSingleton<GGUDPNetworkServer>.instance.StartServer();
		findServersHandler = new FindServersHandler();
		BehaviourSingleton<GGUDPNetworkServer>.instance.AddHandler(findServersHandler);
	}

	private void ShowAllServers(List<Server> servers)
	{
		for (int i = 0; i < servers.Count; i++)
		{
			if (i >= buttons.Count)
			{
				ServerDisplayButton component = NGUITools.AddChild(table.gameObject, serverButtonPrefab).GetComponent<ServerDisplayButton>();
				component.gameObject.name = GGFormat.IndexName(i);
				buttons.Add(component);
			}
			buttons[i].Init(servers[i]);
			buttons[i].gameObject.SetActive(true);
		}
		for (int j = servers.Count; j < buttons.Count; j++)
		{
			buttons[j].gameObject.SetActive(false);
		}
	}

	private void Update()
	{
		List<Server> servers = findServersHandler.servers;
		ShowAllServers(servers);
	}
}
